package com.iseastar.guojiang.task;

import android.os.Bundle;
import android.os.Message;
import android.widget.ListView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.model.TaskDetailBackBean;
import com.shouhuobao.station.R;

import droid.frame.utils.lang.Str;
import droid.frame.utils.sqlite.DateUtils;

/**
 * Created by joker on 2018/1/19.
 * 同城任务详情界面
 */
public class CourierSameCityTaskDetailActivity extends BaseActivity2 {

    private TextView mTaskStatusDescTV;//任务状态描述
    private TextView mTaskDescTV;//任务描述 金额/重量/距离
    private TextView mTakeBuildNameTV;//取的建筑物名称
    private TextView mTakeAddressTV;//取的地址
    private TextView mReceiverBuildNameTV;//送的建筑物名称
    private TextView mReceiverAddressTV;//送的地址
    private TextView mSendContractTV;//寄件人名字
    private TextView mReceiverContractTV;//收件人的名字
    private TextView mTaskCodeTV;//任务编号
    private TextView mTaskSendTimeTV;//派单时间
    private TextView mTaskFinishTimeTV;//任务完成时间

    private ServiceTaskBean historyTaskBean = null;
    private CourierTaskSameCityDetailAdapter mAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        historyTaskBean = (ServiceTaskBean) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
        if (historyTaskBean != null) {
            showLoadingDialog(null);
            AppHttp.getInstance().getHistoryTaskDetail(historyTaskBean.getTaskId());
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_courier_samecity_task_detail);
        super.findViewById();
        getAppTitle().setCommonTitle("任务详情");
        mTaskStatusDescTV = (TextView) findViewById(R.id.task_status_desc_tv);
        mTaskDescTV = (TextView) findViewById(R.id.task_desc_tv);
        mTakeBuildNameTV = (TextView) findViewById(R.id.take_build_name_tv);
        mTakeAddressTV = (TextView) findViewById(R.id.take_address_details_tv);
        mReceiverBuildNameTV = (TextView) findViewById(R.id.send_build_name_tv);
        mReceiverAddressTV = (TextView) findViewById(R.id.send_address_details_tv);
        mSendContractTV = (TextView) findViewById(R.id.send_contract_tv);
        mReceiverContractTV = (TextView) findViewById(R.id.receiver_contract_tv);
        mTaskCodeTV = (TextView) findViewById(R.id.task_code_tv);
        mTaskSendTimeTV = (TextView) findViewById(R.id.send_order_time_tv);
        mTaskFinishTimeTV = (TextView) findViewById(R.id.task_finish_time_tv);
        ListView listView = (ListView) findViewById(R.id.listview);
        mAdapter = new CourierTaskSameCityDetailAdapter(null, getContext(), listView);
        listView.setAdapter(mAdapter);
    }

    /**
     * 更新布局
     */
    private void updateView() {
        if (null == historyTaskBean) {
            return;
        }
        //任务状态描述
        if (historyTaskBean.getStatus() == Const.CourierTaskDoingStatus.TaskFinished) {
            mTaskStatusDescTV.setText("已完成");
        } else if (historyTaskBean.getStatus() == Const.CourierTaskDoingStatus.TaskCancel) {
            mTaskStatusDescTV.setText("支付超时已取消");
        } else if (historyTaskBean.getStatus() == Const.CourierTaskDoingStatus.TaskException) {
            mTaskStatusDescTV.setText("任务异常");
        } else if (historyTaskBean.getStatus() == Const.CourierTaskDoingStatus.TaskCanelForUser) {
            mTaskStatusDescTV.setText("用户取消");
        } else if (historyTaskBean.getStatus() == Const.CourierTaskDoingStatus.TaskCanelForCourier) {
            mTaskStatusDescTV.setText("火箭侠取消");
        }
        mTaskDescTV.setText("￥" + historyTaskBean.getTaskLucre() + "/" + historyTaskBean.getWeight() + "公斤/" + Str.formatDoubleData(historyTaskBean.getDistance() / 1000) + "km");
        mTakeBuildNameTV.setText(historyTaskBean.getBuildName());
        mTakeAddressTV.setText(historyTaskBean.getAddress());
        mReceiverBuildNameTV.setText(historyTaskBean.getGiveBuildName());
        mReceiverAddressTV.setText(historyTaskBean.getGiveAddress());
        mSendContractTV.setText("寄件人：" + historyTaskBean.getContactName());
        mReceiverContractTV.setText("收件人：" + historyTaskBean.getGiveContactName());
        mTaskCodeTV.setText("任务编号：" + historyTaskBean.getTaskNo());
        mTaskSendTimeTV.setText("派单时间：" + DateUtils.format(historyTaskBean.getTaskPostTime()));
        mTaskFinishTimeTV.setText("完成时间：" + DateUtils.format(historyTaskBean.getCompleteTime()));
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.get_history_task_detail:
                cancelLoadingDialog();
                final ReqResult<TaskDetailBackBean> result = JSON.parser(msg.obj, TaskDetailBackBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TaskDetailBackBean backBean = result.getResult();
                            if (null != backBean) {
                                historyTaskBean = backBean.getTask();
                                updateView();
                                mAdapter.setItems(backBean.getDetails(), true);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
